﻿using SimpleInjector;
using System;
using System.Net.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Dispatcher;

namespace Sustainalytics.Utils
{
    public class SimpleInjectorControllerActivator : IHttpControllerActivator
    {

        private readonly Container _container;

        public SimpleInjectorControllerActivator(Container container)
        {
            _container = container;
        }

        public IHttpController Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
        {
            request.RegisterForDispose(SimpleInjector.Lifestyles.AsyncScopedLifestyle.BeginScope(_container));

            return (IHttpController)_container.GetInstance(controllerType);
        }
    }
}
